package com.yuwei.user

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.opengl.Visibility
import android.view.View
import android.view.animation.LinearInterpolator
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.parkingwang.keyboard.KeyboardInputController
import com.parkingwang.keyboard.PopupKeyboard
import com.tencent.mmkv.MMKV
import com.yuwei.base.BaseActivity
import com.yuwei.base.path.RouterPath
import com.yuwei.router.provider.RouteProvider
import com.yuwei.user.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
@Route(path = RouterPath.MODULE_LOGIN_ACTIVITY)
class LoginActivity : BaseActivity<ActivityLoginBinding, UserViewModel>() {
    lateinit var mValueAnimator: ValueAnimator
    override fun initView() {
        binding.loginLayout.login.setOnClickListener {
            if(binding.loginLayout.etUsername.text.toString().isEmpty()||binding.loginLayout.etPassword.text.toString().isEmpty()){
                ToastUtils.showLong("用户名或密码不能为空")
                return@setOnClickListener
            }
            vm.login(binding.loginLayout.etUsername.text.toString(),binding.loginLayout.etPassword.text.toString())
        }
        binding.loginLayout.goRegister.setOnClickListener {
            binding.registerLayout.registerView.visibility = View.VISIBLE
            binding.loginLayout.loginView.visibility = View.GONE
        }
        binding.userTopBar.topBarIcon.visibility = View.GONE
        binding.userTopBar.topBarWhiteNotice.visibility = View.GONE


        binding.registerLayout.register.setOnClickListener {
            if(binding.registerLayout.etPassword.text.toString()!=binding.registerLayout.etRePassword.text.toString()){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            if(binding.registerLayout.etPhone.text.toString().isEmpty()||binding.registerLayout.etPassword.text.toString().isEmpty()||binding.registerLayout.etRePassword.text.toString().isEmpty()){
                ToastUtils.showLong("用户名或密码不能为空")
                return@setOnClickListener
            }
            vm.register(binding.registerLayout.etPhone.text.toString(),binding.registerLayout.etRePassword.text.toString())
        }
        binding.registerLayout.goLogin.setOnClickListener {
            binding.registerLayout.registerView.visibility = View.GONE
            binding.loginLayout.loginView.visibility = View.VISIBLE
        }
        binding.registerLayout.btnCode.setOnClickListener{

            if(!RegexUtils.isMobileExact(binding.registerLayout.etPhone.text.toString())){
                ToastUtils.showLong("请输入正确的手机号")
                return@setOnClickListener
            }
            val code = (100000..999999).random().toString()
            ToastUtils.showLong(code)
            binding.registerLayout.etCode.setText(code)
            binding.registerLayout.btnCode.isClickable = false
            binding.registerLayout.etCode.setText(code)
            mValueAnimator = ValueAnimator.ofInt(15,0)
            mValueAnimator.duration = 15000
            mValueAnimator.interpolator = LinearInterpolator()
            mValueAnimator.addUpdateListener {
                val count = it.animatedValue as Int
                binding.registerLayout.btnCode.text = "${count}秒后重试"
            }
            mValueAnimator.start()
            mValueAnimator.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    binding.registerLayout.btnCode.text = "获取验证码"
                    binding.registerLayout.btnCode.isClickable = true
                }
            })
        }
    }

    override fun initData() {
        vm.loginSuccess.observe(this){
            ToastUtils.showLong("欢迎,${it.username}")
            MMKV.defaultMMKV().encode("userId",it.id)
            MMKV.defaultMMKV().encode("token",it.token)
            ARouter.getInstance().build(RouterPath.MODULE_MAIN_ACTIVITY).navigation()
        }
        vm.registerSuccess.observe(this){
            ToastUtils.showLong("注册成功")
            binding.registerLayout.registerView.visibility = View.GONE
            binding.loginLayout.loginView.visibility = View.VISIBLE
        }
    }

}